package com.daxidi.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daxidi.cn.entity.Manager;
import com.daxidi.cn.mapper.ManagerMapper;
import com.daxidi.cn.service.IManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements IManagerService {
    @Autowired
    private ManagerMapper managerMapper;
    // 添加管理员
    @Override
    public void addManager(Manager manager) {
        managerMapper.insert(manager);
    }
    // 根据管理员id删除管理员
    @Override
    public void deleteManagerById(Integer id) {
        managerMapper.deleteById(id);
    }
    // 根据管理员id修改管理员信息
    @Override
    public void updateManagerId(Manager manager) {
        managerMapper.updateById(manager);
    }
    // 查询所有管理员
    @Override
    public Map selectManagersPage(Integer pageNum, Integer pageSize,String managerName,String beginTime,String endTime) {
        Page<Manager> page = new Page<Manager>(pageNum, pageSize);
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.like(managerName!=null,"manager_name",managerName);
        if (beginTime != null && endTime != null && !"".equals(beginTime) && !"".equals(endTime)) {
            wrapper.between("created_at", beginTime, endTime);
        }
        Page<Manager> managerPage = managerMapper.selectPage(page, wrapper);
        long total = managerPage.getTotal();
        List<Manager> managers = managerPage.getRecords();
        Map map = new HashMap();
        map.put("data",managers);
        map.put("count",total);
        return map;

    }
    // 管理员登录
    @Override
    public Manager loginManager(String managerName, String password) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("username",managerName);
        wrapper.eq("passwrod",password);
        Manager manager = managerMapper.selectOne(wrapper);
        return manager;
    }

    @Override
    public Manager selectManagerById(Long id) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Manager manager = managerMapper.selectOne(wrapper);
        return manager;
    }

    @Override
    public Manager selectManagerByName(String name) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("username",name);
        Manager manager = managerMapper.selectOne(wrapper);
        return manager;
    }
}
